package com.wwwarehouse.taskcenter.fragment.warehouse_connect;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSONObject;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CardDeskFunctionResponseBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.http.NetUtils;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.ScanConnectCodeBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;
import com.wwwarehouse.taskcenter.fragment.warehouse_reg.WhouseRegFirstFragment;
import com.wwwarehouse.taskcenter.fragment.warehouse_reg.WhouseRegSecFragment;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;


/**
 * Created by clannad.shao on 2017/11/24.
 * Description:
 */
@Route(path = TaskCenterConstant.scanConnectCodeRoute)
public class ScanStationCodeFragment extends BaseFragment implements View.OnClickListener{

    private EditText idScanCodeEt;
    private Button idScanCodeBt;
    private Map<String, Object> map;
    private String jobPointUkid;
    private String businessId;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.whouse_connect_scancode, container, false);
    }

    /**
     * initView
     *
     * @param savedInstanceState
     */
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        idScanCodeEt = $(R.id.idScanCodeEt);
        idScanCodeBt = $(R.id.idScanCodeBt);
        idScanCodeBt.setOnClickListener(this);
    }




    /**
     * onclick
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.idScanCodeBt){
            //点击请求跳转
            request(0, TaskCenterConstant.SCAN_CONNECT_CODE, map);
        }
    }

    private void request(int what, String url, Map<String, Object> map){
        if(!NetUtils.isHttpConnected(mActivity)){
            toast(getString(R.string.warehouse_reg_no_network));
        }
        NoHttpUtils.httpPost(url, map, new NoHttpUtils.OnResponseListener() {
            @Override
            public void onSucceed(CommonClass commonClass, int what) {
                ScanConnectCodeBean scanConnectCodeBean = JSONObject.parseObject(commonClass.getData().toString(), ScanConnectCodeBean.class);
                Fragment fragment = new WhouseRegFirstFragment();
                Bundle bundle = new Bundle();
                bundle.putString("jobPointUkid", jobPointUkid);
                bundle.putString("businessId", businessId);
                bundle.putString("userId", sp.getValue(Constant.sp_User_Id));
                fragment.setArguments(bundle);
                pushFragment(fragment);
            }

            @Override
            public void onFailed(String result, int what) {

            }

            @Override
            public void onFinish(int what) {

            }
        }, what);

    }




    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof WhouseRegSecFragment) {
            mActivity.setTitle(getString(R.string.warehouse_reg_title));
        }
    }


    @Override
    public void requestDatas() {
        Bundle bundle = getArguments();
        map = new HashMap<>();
        if(bundle != null){
            CardDeskFunctionResponseBean.TaskBean taskBean = (CardDeskFunctionResponseBean.TaskBean) bundle.getSerializable(Constant.KEY_CARD_DESK_FUNCTION_DETAILS);
//            businessId = taskBean.getBusinessId();
            businessId = "10001507";
            taskBean.getTaskTypeUkid();
            String userId = sp.getValue(Constant.sp_User_Id);
            jobPointUkid = "12152600000002015";
            map.put("businessId", businessId);
            map.put("userId", "66600005");
            map.put("jobPointUkid", jobPointUkid);
        }
    }
}
